﻿using System;
using VIT.Library.Web;
using VIT.Web.Controls;
using VIT.Provider;
using System.Data;
using System.Collections.Generic;

namespace VIT.Web.Admin
{
    public partial class LogoEdit : VIT.Web.UI.VITComponent
    {
        Provider.Logo obj;
        protected void Page_Load(object sender, EventArgs e)
        {
            obj = new Provider.Logo();

            try
            {
                obj.Id = int.Parse(Request["sId"]);
            }
            catch
            {
                obj.Id = -1;
            }

            if (obj.Id < 0) ButtonInsert.Visible = true; else ButtonModify.Visible = true;

            if (!IsPostBack)
            {
                loadLanguages();
                loadPositions();
                loadData();
            }
        }

        #region Methods
        private void loadData()
        {
            obj.LanguageId = Session["language"].ToString();
            obj.GetSingle();
            if (obj.Id > 0)
            {
                txtTitle.Text = obj.Title;
                txtLink.Text = obj.Link;                
                txtDescription.Text = obj.Description;
                if (!string.IsNullOrEmpty(obj.FileName))
                {
                    img.Visible = true;
                    img.ImageUrl = URLRoot + "Uploads/Images/Logos/" + obj.FileName;
                    img.AlternateText = obj.FileName;
                }
                ddlPositon.SelectedValue = obj.PositionId;
            }
        }

        private void loadLanguages()
        {
            ddlLang.DataSource = new Language().getAll();
            ddlLang.DataTextField = "Name";
            ddlLang.DataValueField = "Id";
            ddlLang.DataBind();

            ddlLang.SelectedValue = Session["language"].ToString(); 
        }

        private void loadPositions()
        {
            ddlPositon.DataTextField = "Name";
            ddlPositon.DataValueField = "Id";
            ddlPositon.DataSource = new Provider.Position().getAll();
            ddlPositon.DataBind();     
        }

        private void save()
        {
            // bat loi tren server
            var data = new Dictionary<dynamic, List<object>> {
                                                        {txtTitle, new List<object> {"required"}},
                                                        {txtOrder, new List<object> {"number"}}
                                                    };
            if (MainCore.checkData(data))
            {
                bool checkType = true;
                obj.PositionId = ddlPositon.SelectedValue;

                if (fupImg.HasFile)
                {
                    if (MainCore.checkFile(fupImg.FileName, "isImage"))
                    {
                        if (rblType.SelectedValue == "Image")
                        {
                            string oldImg = img.AlternateText;
                            obj.FileName = String.Format("{0:yyMMddhhmmss}", DateTime.Now) + System.IO.Path.GetExtension(fupImg.FileName);
                            string pathFile = "/Uploads/Images/Logos/" + obj.FileName;
                            try
                            {
                                fupImg.SaveAs(URLBaseDir + pathFile);
                                obj.FullLogo = (URLRoot + pathFile).genImage();
                                if (System.IO.File.Exists(URLBaseDir + "Uploads/Images/Logos/" + oldImg))
                                    System.IO.File.Delete(URLBaseDir + "Uploads/Images/Logos/" + oldImg);
                            }
                            catch (System.NotSupportedException ex)
                            {
                                throw ex;
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                        else checkType = false;
                    }
                    else if (MainCore.checkFile(fupImg.FileName, "isFlash"))
                    {
                        if (rblType.SelectedValue == "Flash")
                        {
                            obj.FileName = obj.Id + System.IO.Path.GetExtension(fupImg.FileName);
                            string pathFile = "/Uploads/Images/Logos/" + obj.FileName;
                            fupImg.SaveAs(URLBaseDir + pathFile);

                            obj.FullLogo = (URLRoot + pathFile).genFlash(int.Parse(txtWidth.Text), int.Parse(txtHeight.Text));
                        }
                        else checkType = false;
                    }
                    else checkType = false;
                }
                else obj.FileName = img.AlternateText;

                if (checkType)
                {
                    obj.LanguageId = ddlLang.SelectedValue;
                    obj.Title = txtTitle.Text;
                    obj.Link = txtLink.Text;
                    obj.Description = txtDescription.Text;
                    obj.Type = rblType.SelectedValue;
                    obj.Orders = int.Parse(txtOrder.Text);
                    obj.Published = chkPublish.Checked;

                    if (obj.Save(true)) setTrueVisible(lblThanhCong);
                    else setTrueVisible(lblThatBai);
                }
                else setTrueVisible(lblHinhKoDung);
            }
            else setTrueVisible(lblThatBai);
        }

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            lblThanhCong.Visible = false;
            lblThatBai.Visible = false;
            lblHinhKoDung.Visible = false;

            control.Visible = true;
        }
        #endregion

        #region Events
        protected void Save_Click(object sender, EventArgs e)
        {
            save();
            RedirectComponent("Logo", "LogoList");
        }

        protected void Apply_Click(object sender, EventArgs e)
        {
            save();
            loadData();
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            RedirectComponent("Logo", "LogoList");
        }

        protected void rblType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rblType.SelectedValue == "Image") pnSize.Visible = false;
            else pnSize.Visible = true;
            loadData();
        }
        #endregion
    }
}
